package util;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2018/2/8.
 */
public class Selector<TK, TV> {

    public static void main(String args[]) {

        String result = (String) new Selector<String, String>()
                .putCatalog("4301", "发证").putCatalog("4302", "延续").
                        putCatalog("4303", "许可范围跟变").run("4302");

        System.out.println(result);

      Object o  =new Selector().putCatalog("4301", 1).putCatalog("4302", 2).
                        putCatalog("4303", 2).run("123","uu");
        System.out.println(o);



    }


    private Map<TK, TV> catalogMap = new HashMap<TK, TV>();


    public TV run(TK key, TV defaultValue) {
        if (catalogMap == null) {
            return defaultValue;
        } else {
            if (catalogMap.containsKey(key)) {
                return catalogMap.get(key);
            } else {
                return defaultValue;
            }
        }
    }

    public TV run(TK key) {
       return  run(key, null);
    }

    public Selector putCatalog(TK key, TV value) {
        initCatalogMap();
        catalogMap.put(key, value);
        return this;
    }

    public Selector removeCatalog(TK key) {
        initCatalogMap();
        catalogMap.remove(key);
        return this;
    }

    public Selector clearCateLog() {
        if (catalogMap != null) {
            catalogMap.clear();
        }
        return this;
    }

    private void initCatalogMap() {
        if (catalogMap == null) {
            catalogMap = new HashMap<TK, TV>();
        }
    }


    /****************************************
     * 构造 方法
     ****************************************/

    public Selector(Map<TK, TV> catalogMap) {
        this.catalogMap = catalogMap;
    }

    public Selector() {
    }


    /****************************************
     * get set 方法
     ****************************************/
    public Map<TK, TV> getCatalogMap() {
        return catalogMap;
    }

    public void setCatalogMap(Map<TK, TV> catalogMap) {
        this.catalogMap = catalogMap;
    }
}
